package com.example.test;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.test.utils.Http;
import com.example.test.utils.MyApplication;
import com.example.test.utils.Sign;

import org.json.JSONObject;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

import static java.util.Objects.*;

public class studentLoginActivity extends AppCompatActivity {
    private Button student;
    boolean status;
    private EditText username;
    private EditText password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_studentlogin);
        init();
        student.setOnClickListener(new MyOnClickListener());
    }

    public void gotoSign(View view){
        Intent intent=new Intent(studentLoginActivity.this, Sign.class);
        startActivity(intent);
    }
    public void get()
    {
//        Intent intent=new Intent(studentLoginActivity.this,com.example.test.student.MainActivity.class);
//        Bundle bundle=new Bundle();
//        bundle.putBoolean("status",false);
//        intent.putExtras(bundle);
//        startActivity(intent);
        String Url = dataUpdate();
        if(Url.equals("false")){
            Toast.makeText(studentLoginActivity.this,"不能为空",Toast.LENGTH_LONG).show();
        } else {
//            status = false;
            Login(Url);
        }
    }

    private void Login(final String url) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String res = Http.sendGet(url);
                final LinkedHashMap<String, Object> hashMaps = Http.stringToJsonObject(res);
//                Log.d("HttpGET", (String) hashMaps.get("msg"));
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        assert hashMaps != null;
                        Toast.makeText(studentLoginActivity.this,(String)hashMaps.get("msg"),Toast.LENGTH_LONG).show();

                        if ((int)hashMaps.get("code") == 1){
                            Map result = (Map) hashMaps.get("result");
                            MyApplication.setGlobalID(result.get("id").toString());
                            gotoMain();
//                            Log.d("裂开",(String) result.get("id"));
                        }
                    }
                });
            }
        }).start();

    }
    public void gotoMain(){
        Intent intent=new Intent(studentLoginActivity.this,com.example.test.student.MainActivity.class);
        //启动
        startActivity(intent);
    }

    public String dataUpdate(){
        String[] data = {username.getText().toString(),password.getText().toString(),MyApplication.getGlobalvariable()};
        String[] dataName = {"username","password","action"};
        StringBuilder url = new StringBuilder("http://36428pn632.qicp.vip/student/index.php/user/login");
        for(int i = 0; i < data.length; i++){
            if(data[i].equals("")){
                return "false";
            }
            if(i == 0){
                url.append("?").append(dataName[i]).append("=").append(data[i]);
            } else {
                url.append("&").append(dataName[i]).append("=").append(data[i]);
            }
        }
        return url.toString();
    }
    class MyOnClickListener  implements View.OnClickListener
    {

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.student_button: get();
                    break;

            }
        }
    }
    public void init(){
        student = findViewById(R.id.student_button);
        username = findViewById(R.id.login_username);
        password = findViewById(R.id.login_password);
//        Bundle bundle = this.getIntent().getExtras();
//        status = bundle.getBoolean("status");
        if (MyApplication.getGlobalvariable().equals("teacher")){
            TextView title = findViewById(R.id.textView3);
            title.setText("教师登陆");
        }

    }
}